package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TravelCommendServiceImpl implements ITravelCommendService {
    @Autowired
    private TravelCommendMapper commendMapper;


    @Override
    public void saveOrUpdate(TravelCommend commend) {
        if (commend.getId() == null) {
            commendMapper.insert(commend);
        } else {
            commendMapper.updateByPrimaryKey(commend);
        }
    }

    @Override
    public PageInfo list(TravelCommendQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<TravelCommend> list = commendMapper.selectAll(qo);
        return new PageInfo(list);
    }

    @Override
    public void selectByTravelId(Long travelId) {
        TravelCommend commend = commendMapper.selectByTravelId(travelId);
        if (commend!=null) {
            throw new RuntimeException("该游记已经被推荐");
        }
    }

    @Override
    public PageInfo<Map> selectForCommend(TravelCommendQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = commendMapper.selectCommendForIndex(qo);
        return new PageInfo(list);
    }
}
